import math
import matplotlib.pyplot as plt
import numpy as np

# 向量
def vector(start_p,end_p):
    x = end_p[0] - start_p[0]
    # y = end_p[1] - end_p[1] #之前的
    y = end_p[1] - start_p[1]
    return (x,y)

# 投影常量
def projection(vector_a,vector_b):
    return vector_a[0] * vector_b[0] + vector_a[1] * vector_b[1]

# 巷道实际长度
def tunnelLength(x,y):
    a = x[1] - x[0]
    b = y[1] - y[0]
    c = math.sqrt(a*a+b*b)
    return c
    
# 巷道实际点
def finalPoint(x,y,dot_p):
    v_t = vector((x[0],y[0]),(x[1],y[1]))
    dot_t = projection(v_t,v_t)

    mu = dot_p/dot_t
    print(mu)

    result = (x[0]+v_t[0]*mu,y[0]+v_t[1]*mu)

    return result

# 设置中文字体支持（可选）
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

x = (1,8)
y = (3,7)

# 创建图形
plt.figure(figsize=(8, 4))  # 设置图形大小

# 绘制折线图
plt.plot(x, y, label='线性增长', color='blue', linestyle='-', marker='o')

plt.plot(3,6,'ro')
# plt.plot(4,8,'ro')

plt.arrow(1,3,3-1,6-3,fc='blue',ec='red',head_width=0.2,head_length=0.2)

v_t = vector((x[0],y[0]),(x[1],y[1]))
v_p = vector((x[0],y[0]),(3,6))
projectionResult = projection(v_t,v_p)
s_pro = math.sqrt(projectionResult)
print(s_pro)
f_p = finalPoint(x,y,projectionResult)
plt.plot(f_p[0],f_p[1],'ro')


# 添加标题和标签
plt.title('简单折线图示例')
plt.xlabel('X轴')
plt.ylabel('Y轴')

# 添加图例
plt.legend()

# 显示网格
plt.grid(True, linestyle='--', alpha=0.5)

# 显示图形
plt.show()